package cn.com.chec.en.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.UserInfoDao;
import cn.com.chec.en.model.domain.RoleInfo;
import cn.com.chec.en.model.domain.UserInfo;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.SecurityMD5;

@Controller
@RequestMapping({ "/admin/user" })
public class UserAdminController {
	protected static final Logger logger = Logger
			.getLogger(UserAdminController.class);

	@Resource
	private UserInfoDao userInfoDao;

	@RequestMapping({ "/showPersonalUpdate" })
	public ModelAndView showPersonalUpdate(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/userInfo/personalOpr");

		List<RoleInfo> roleList = this.userInfoDao.getRoleList(0, 2147483647);
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : 0;
		UserInfo userInfo = this.userInfoDao.getUser(userId);

		map.put("roleList", roleList);
		map.put("userInfo", userInfo);
		map.put("cate", "update");
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/personalUpdate" })
	public ModelAndView personalUpdate(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : 0;
		String newPwd = request.getParameter("password");
		UserInfo userInfo = this.userInfoDao.getUser(userId);
		if (StringUtils.isNotBlank(newPwd)) {
			userInfo.setPassword(SecurityMD5.crypt(newPwd));
		}
		userInfo.setId(userId);
		userInfo.setUsername(request.getParameter("username"));
		userInfo.setRealName(request.getParameter("realName"));
		userInfo.setDeptName(request.getParameter("deptName"));
		userInfo.setTelphone(request.getParameter("telphone"));
		userInfo.setMobilephone(request.getParameter("mobilephone"));
		userInfo.setEmail(request.getParameter("email"));

		if (this.userInfoDao.updateUser(userInfo)) {
			message = "更新用户成功!";

			HttpSession session = request.getSession();
			UserInfo userInfoSession = (UserInfo) session
					.getAttribute("systemUserInfo");
			int sessionUserId = userInfoSession.getId();
			if (sessionUserId == userInfo.getId()) {
				session.removeAttribute("systemUserInfo");
				session.setAttribute("systemUserInfo",
						this.userInfoDao.getUser(userInfo.getId()));
			}
		} else {
			message = "更新用户失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/list" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/userInfo/userList");
		List<RoleInfo> roleInfoList = this.userInfoDao.getRoleList(0,
				2147483647);
		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		int roleId = StringUtils.isNotBlank(request.getParameter("roleId")) ? Integer
				.parseInt(request.getParameter("roleId")) : -1;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<UserInfo> userInfoList = this.userInfoDao.getUserListByRoleId(
				roleId, pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.userInfoDao.getUserCountByRoleId(roleId));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("roleInfoList", roleInfoList);
		map.put("userInfoList", userInfoList);
		map.put("pageBean", pageBean);
		map.put("roleId", Integer.valueOf(roleId));

		mav.addAllObjects(map);
		return mav;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/showInsert" })
	public ModelAndView showInsert(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/userInfo/userOpr");
		List<RoleInfo> roleInfoList = this.userInfoDao.getRoleList(0,
				2147483647);
		map.put("roleInfoList", roleInfoList);
		map.put("userInfo", new UserInfo());
		map.put("act", "insert");
		map.put("userRightsSession",
				(List<String>) session.getAttribute("systemUserRigthList"));
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/insert" })
	public ModelAndView insert(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		int roleId = StringUtils.isNotBlank(request.getParameter("roleId")) ? Integer
				.parseInt(request.getParameter("roleId")) : 0;
		UserInfo userInfo = new UserInfo();
		userInfo.setUsername(request.getParameter("username"));
		userInfo.setPassword(SecurityMD5.crypt(request.getParameter("password")));
		userInfo.setRealName(request.getParameter("realName"));
		userInfo.setDeptName(request.getParameter("deptName"));
		userInfo.setTelphone(request.getParameter("telphone"));
		userInfo.setMobilephone(request.getParameter("mobilephone"));
		userInfo.setEmail(request.getParameter("email"));
		userInfo.setRoleId(roleId);
		if (this.userInfoDao.createUser(userInfo))
			message = "创建用户成功!";
		else {
			message = "创建用户失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/showUpdate" })
	public ModelAndView showUpdate(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/userInfo/userOpr");
		List<RoleInfo> roleInfoList = this.userInfoDao.getRoleList(0,
				2147483647);
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : 0;
		UserInfo userInfo = this.userInfoDao.getUser(userId);
		map.put("roleInfoList", roleInfoList);
		map.put("userInfo", userInfo);
		map.put("act", "update");
		map.put("userRightsSession",
				(List<String>) session.getAttribute("systemUserRigthList"));
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/update" })
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";

		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : 0;
		int roleId = StringUtils.isNotBlank(request.getParameter("roleId")) ? Integer
				.parseInt(request.getParameter("roleId")) : 0;
		UserInfo userInfo = this.userInfoDao.getUser(userId);
		String newPwd = request.getParameter("password");
		if (StringUtils.isNotBlank(newPwd)) {
			userInfo.setPassword(SecurityMD5.crypt(newPwd));
		}
		userInfo.setId(userId);
		userInfo.setUsername(request.getParameter("username"));
		userInfo.setRealName(request.getParameter("realName"));
		userInfo.setDeptName(request.getParameter("deptName"));
		userInfo.setTelphone(request.getParameter("telphone"));
		userInfo.setMobilephone(request.getParameter("mobilephone"));
		userInfo.setEmail(request.getParameter("email"));
		userInfo.setRoleId(roleId);
		if (this.userInfoDao.updateUser(userInfo)) {
			message = "更新用户成功!";

			int sessionUserId = ((UserInfo) session
					.getAttribute("systemUserInfo")).getId();
			if (sessionUserId == userInfo.getId()) {
				session.removeAttribute("systemUserInfo");
				session.setAttribute("systemUserInfo",
						this.userInfoDao.getUser(userInfo.getId()));
			}
		} else {
			message = "更新用户失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/delUser" })
	public ModelAndView delUser(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : 0;
		if (this.userInfoDao.delUser(userId))
			message = "删除用户成功!";
		else {
			message = "删除用户失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/resetPwd" })
	public ModelAndView resetPwd(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : 0;
		if (this.userInfoDao.resetUserPwd("admin", userId)) {
			message = "重置密码成功,新密码为\"admin\",请妥善保管!";

			int sessionUserId = ((UserInfo) session
					.getAttribute("systemUserInfo")).getId();
			if (sessionUserId == userId) {
				session.removeAttribute("systemUserInfo");
				session.setAttribute("systemUserInfo",
						this.userInfoDao.getUser(userId));
			}
		} else {
			message = "重置密码失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}
}